package com.bfb;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.bfb.DishTypeActivity.DishTypeAsyncTask;
import com.bfb.helpers.Dishes;
import com.bfb.helpers.Ingredients;
import com.bfb.helpers.Restaurants;
import com.customComponents.PromptDialog;

/**
 * This is the {@link Activity} that handles the creation of a new dish. The
 * content layout for the activity is in the xml layout file create_dish.xml. It
 * implements some Listeners for items used inside the activity. The implemented
 * listeners are {@link OnEditorActionListener}, {@link OnItemClickListener},
 * {@link OnClickListener} and {@link android.view.View.OnClickListener}.
 * 
 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
 * 
 */
public class CreateDishActivity extends Activity implements
		OnEditorActionListener, OnItemClickListener, OnClickListener,
		android.view.View.OnClickListener {
	private static final int GALLERY = 0;
	private static final int CAMERA = 1;
	private static final int MAP = 2;

	String dishName;

	/** The root {@link LinearLayout} */
	LinearLayout createDishForm;

	/** The {@link ImageView} that will hold the new dish image */
	ImageView dishImageView;
	Bitmap dishImage;

	/** The text view that will hold the dish name */
	public TextView dishNameTextView;

	/** The {@link RatingBar} for the dish rating */
	RatingBar dishRatingBar;

	/**
	 * The restaurant name is inserted into an {@link AutoCompleteTextView} to
	 * avoid having duplicate entries for a restaurant in the db
	 */
	AutoCompleteTextView restaurantNameField;
	ArrayAdapter<String> restaurantAutoCompleteAdapter;

	/**
	 * The ingredient names are inserted into an {@link AutoCompleteTextView}
	 * because the user is only allowed to select from the ingredients in the
	 * db.
	 */
	AutoCompleteTextView ingredientNameField;
	ArrayAdapter<String> ingredientNameAutoCompleteAdapter;

	/**
	 * This is the {@link ArrayList} that keeps track of the ingredients that
	 * the user has selected.
	 */
	ArrayList<String> selectedIngredients;

	/** A {@link Spinner} (drop down list) to select the dish type. */
	Spinner dishTypeSpinner;

	/** For storing the locations retrieved from the map */
	double[] longitudes;
	double[] latitudes;

	/**
	 * Overridden onCreate method. This is where the contentView is set and all
	 * the views are initialized.
	 * 
	 * @param savedInstanceState
	 *            This parameter will hold the saved state for this activity in
	 *            case it has been interrupted and its state is saved to avoid
	 *            having to fill all the data again.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_dish);
		dishImageView = (ImageView) findViewById(R.id.new_dish_photo);
		dishNameTextView = (TextView) findViewById(R.id.new_dish_name);
		dishRatingBar = (RatingBar) findViewById(R.id.new_dish_rating);
		createDishForm = (LinearLayout) findViewById(R.id.create_dish_form);

		// if (savedInstanceState == null) {

		initDishTypeSpinner();
		initRestaurantNameField();
		initIngredientsField();

		// } else {
		// onRestoreInstanceState(savedInstanceState);
		// }
	}

	/**
	 * Overridden onCreateOptionsMenu method. This is where the {@link Menu} for
	 * the {@link Activity} is inflated. The menu items are defined in the xml
	 * file create_dish_menu.xml in the res/menu folder.
	 * 
	 * @param menu
	 *            This parameter will hold the menu into which we will inflate
	 *            the desired layout for the menu using a {@link MenuInflater}.
	 * @return boolean
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.create_dish_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Overridden onOptionsItemSelected method. This is the listener for menu
	 * item selection. It takes an input the selected item. We have 2
	 * {@link MenuItem} in the this {@link Activity}. The first is to save the
	 * dish and the second is to cancel the creation of the new dish.
	 * 
	 * @param item
	 *            This parameter will hold the {@link MenuItem} that has been
	 *            selected.
	 * @return boolean
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.save_new_dish_menuitem) {
			saveDish();
		} else {
			if (item.getItemId() == R.id.cancel_menuitem) {
				showCancelConfirmationDialog();
			}
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * This method is called when the "Cancel" {@link MenuItem} is clicked. It
	 * will show a confirmation {@link PromptDialog} with the options "yes" and
	 * "no".
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	private void showCancelConfirmationDialog() {
		PromptDialog dlg = new PromptDialog(CreateDishActivity.this,
				R.string.clear_confirm_title,
				R.string.cancel_confirmation_message, true, R.string.yes,
				R.string.no) {
			@Override
			public boolean onOkClicked() {
				onBackPressed();
				return true;
			}
		};
		dlg.show();
	}

	/**
	 * Shows the {@link PromptDialog} for missing required fields validation.s
	 * 
	 * @param messageStringId
	 *            Contains the resource string id that is relevant to the
	 *            missing field being validated.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	private void showMissingFieldsDialog(int messageStringId) {
		PromptDialog dlg = new PromptDialog(CreateDishActivity.this,
				R.string.missing_info, messageStringId, false, -1, R.string.ok) {
			@Override
			public boolean onOkClicked() {
				return true;
			}
		};
		dlg.show();
	}

	/**
	 * Checks whether all the required fields are filled. If yes, then it calls
	 * the {@link Dishes}.create() method with all the input from the user which
	 * will send it to the server. If no, then it calls the method to show the
	 * validation dialog.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	public void saveDish() {
		String dishName = dishNameTextView.getText().toString();
		if (dishName == null || dishName.trim().equals("")) {
			showMissingFieldsDialog(R.string.dish_name_required);
			return;
		}

		String dishType = dishTypeSpinner.getSelectedItem().toString();
		if (dishType == null || dishType.trim().equals("")) {
			showMissingFieldsDialog(R.string.dish_type_required);
			return;
		}

		String restaurantName = restaurantNameField.getText().toString();
		if (restaurantName == null || restaurantName.trim().equals("")) {
			showMissingFieldsDialog(R.string.restaurant_name_required);
			return;
		}

		String restaurantIsNew = "no";
		if (restaurantAutoCompleteAdapter.getPosition(restaurantName) == -1) {
			if (latitudes == null || latitudes.length == 0) {
				showMissingFieldsDialog(R.string.restaurant_location_required);
				return;
			}
			restaurantIsNew = "yes";
		}

		float dishRating = dishRatingBar.getRating();

		if (dishImage == null) {
			dishImage = BitmapFactory.decodeResource(getResources(),
					R.drawable.default_dish);
		}

		dishType = getResources().getStringArray(R.array.DishTypeNames2)[dishTypeSpinner
				.getSelectedItemPosition()];
		Dishes.create(dishName, dishType, restaurantName, longitudes,
				latitudes, restaurantIsNew, dishRating, selectedIngredients,
				dishImage);
		Toast.makeText(this, getString(R.string.dish_created_successfully),
				Toast.LENGTH_LONG).show();
		finish();
	}

	/**
	 * Initializes the ingredients {@link AutoCompleteTextView}. It retrieves
	 * the list of ingredients from the server in an {@link AsyncTask} that is
	 * implemented here as {@link DishTypeAsyncTask}. After the list is
	 * retrieved, it is bound to the {@link ArrayAdapter} of the view. Then it
	 * sets an {@link OnItemClickListener} and {@link OnEditorActionListener}
	 * listener as {@link CreateDishActivity}.this.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	private void initIngredientsField() {
		selectedIngredients = new ArrayList<String>();
		ingredientNameField = (AutoCompleteTextView) findViewById(R.id.new_dish_ingredients_autocomplete);
		ArrayList<String> allIngredients = new ArrayList<String>();
		try {
			allIngredients = new IngredientsAsyncTask().execute().get();
		} catch (Exception e) {
			Toast.makeText(this, getString(R.string.error_getting_ingredients),
					Toast.LENGTH_LONG).show();
		}
		ingredientNameAutoCompleteAdapter = new ArrayAdapter<String>(this,
				R.layout.list_item, allIngredients);
		ingredientNameField.setAdapter(ingredientNameAutoCompleteAdapter);
		ingredientNameField.setOnItemClickListener(this);
		ingredientNameField.setOnEditorActionListener(this);
	}

	/**
	 * Initializes the restaurants {@link AutoCompleteTextView}. It retrieves
	 * the list of restaurants from the server in an {@link AsyncTask} that is
	 * implemented here as {@link RestaurantAsyncTask}. After the list is
	 * retrieved, it is bound to the {@link ArrayAdapter} of the view.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	private void initRestaurantNameField() {
		restaurantNameField = (AutoCompleteTextView) findViewById(R.id.new_dish_restaurant_name);
		ArrayList<String> allRestaurants = new ArrayList<String>();
		try {
			allRestaurants = new RestaurantAsyncTask().execute().get();
		} catch (Exception e) {
			Toast.makeText(this, getString(R.string.error_getting_restaurants),
					Toast.LENGTH_LONG).show();
		}
		restaurantAutoCompleteAdapter = new ArrayAdapter<String>(this,
				R.layout.list_item, allRestaurants);
		restaurantNameField.setAdapter(restaurantAutoCompleteAdapter);
	}

	/**
	 * Initializes the dish types {@link Spinner}. It retrieves the list of dish
	 * types from the resources. After the list is retrieved, it is bound to the
	 * {@link ArrayAdapter} of the view.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	private void initDishTypeSpinner() {
		dishTypeSpinner = (Spinner) findViewById(R.id.new_dish_type);
		ArrayAdapter<CharSequence> dishTypeSpinnerAdapter = ArrayAdapter
				.createFromResource(this, R.array.DishTypeNames1,
						android.R.layout.simple_spinner_item);
		dishTypeSpinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		dishTypeSpinner.setAdapter(dishTypeSpinnerAdapter);
	}

	/**
	 * This calls a new {@link Intent} for showing the {@link RestaurantMap}.
	 * This view will allow the user to pick the location of the restaurant off
	 * the map.
	 * 
	 * @param v
	 *            The view that was clicked in order for this method to be
	 *            called. In this case it is the pin button next to the
	 *            restaurant text field.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	public void setRestaurantLocation(View v) {
		Intent intent = new Intent(CreateDishActivity.this, RestaurantMap.class);
		intent.putExtra("rest_name", restaurantNameField.getText().toString());
		intent.putExtra("rest_id", "");
		startActivityForResult(intent, MAP);
	}

	/**
	 * This is the method called when the user clicks on the image of the dish.
	 * It will generate an {@link AlertDialog} where the user can choose from
	 * where to get the image. The choices are either Camera or Gallery.
	 * 
	 * @param v
	 *            The view that was clicked in order for this method to be
	 *            called. In this case it is the image view of the dish.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	public void chooseImageSource(View v) {
		final CharSequence[] items = { getString(R.string.gallery),
				getString(R.string.camera) };

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.choose_image_source);
		builder.setItems(items, this);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Calls the intent for starting the Camera using {@link MediaStore}
	 * .ACTION_IMAGE_CAPTURE. It will then use startActivityForResult and give
	 * the request code as CAMERA. This code will be used later to retrieve the
	 * photo taken.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	protected void startCamera() {
		Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(camIntent, CAMERA);
	}

	/**
	 * Calls the intent for starting the Gallery using {@link MediaStore}
	 * .ACTION_GET_CONTENT. The intent will be restricted to only images. It
	 * will then use startActivityForResult and give the request code as
	 * GALLERY. This code will be used later to retrieve the photo selected.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	protected void startGallery() {
		Intent gallIntent = new Intent(Intent.ACTION_GET_CONTENT);
		gallIntent.setType("image/*");
		startActivityForResult(gallIntent, GALLERY);
	}

	/**
	 * Called when the fired activity with a result returns. Here it checks
	 * whether the activity was the gallery, the camera or the map. If it is the
	 * gallery or the camera then it calls the method getImageFromURI which will
	 * retrieve the image from the returned URI in the result of the activity.
	 * If it is the map, the method should retrieve the chosen location by the
	 * user from the activity result.
	 * 
	 * @param requestCode
	 *            The code of the activity that was fired. This is set when we
	 *            call startActivityForResult.
	 * @param resultCode
	 *            The result code which can either be RESULT_OK or RESULT_CANCEL
	 *            or RESULT_FIRST_USER.
	 * @param data
	 *            This is the {@link Intent} that will hold the result of the
	 *            activity.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == GALLERY || requestCode == CAMERA) {
			if (resultCode == RESULT_OK) {
				dishImage = getImageFromURI(data.getData());

				if (dishImage != null) {
					dishImageView.setImageBitmap(dishImage);
				} else {
					Toast.makeText(getBaseContext(),
							getString(R.string.image_error), Toast.LENGTH_LONG)
							.show();
				}
			} else {
				if (resultCode != RESULT_CANCELED) {
					Toast.makeText(getBaseContext(), getString(R.string.error),
							Toast.LENGTH_LONG).show();
				}
			}
		} else {
			if (requestCode == MAP) {
				Bundle inputData = data.getExtras();
				String restName = inputData.getString("restaurantName");
				longitudes = inputData.getDoubleArray("longitudes");
				latitudes = inputData.getDoubleArray("latitudes");
				if (restName != null && !restName.trim().equals("")) {
					restaurantNameField.setText(restName);
				}
			}
		}
	}

	/**
	 * This method retrieves a Bitmap image from its URI provided by the
	 * activity that returned. This Bitmap is retrieved using {@link Media}
	 * .getBitmap.
	 * 
	 * @param data
	 *            The {@link Uri} of the image.
	 * @return Bitmap
	 */
	private Bitmap getImageFromURI(Uri data) {
		try {
			return Media.getBitmap(getContentResolver(), data);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * This is the {@link OnItemClickListener} for the ingredients
	 * {@link AutoCompleteTextView}. It will get the selected text and add an
	 * ingredient to the view. It will also remove the selected ingredient from
	 * the adapter to avoid choosing the same ingredient twice.
	 * 
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		String selected = ingredientNameAutoCompleteAdapter.getItem(position);
		addIngredientToView(selected);
		ingredientNameField.setText("");
		ingredientNameAutoCompleteAdapter.remove(selected);
	}

	/**
	 * This method will check whether this ingredient has been selected before.
	 * If yes, it will display a {@link Toast} message to notify the user.
	 * Otherwise, it will inflate a layout for the ingredient_item.xml. This
	 * layout consists of a {@link TextView} and an {@link ImageView}. The text
	 * view will hold the name of the ingredient and the image view will be a
	 * close button that when clicked will remove the ingredient from the
	 * selected list. This inflated view is then added to the root layout which
	 * is scrollable so that every new ingredient added will be displayed to the
	 * user in a list like form.
	 * 
	 * @param selected
	 *            The name of the ingredient that has been selected.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	private void addIngredientToView(String selected) {
		if (!selectedIngredients.contains(selected)) {
			selectedIngredients.add(selected);
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			LinearLayout ingredientItem = (LinearLayout) inflater.inflate(
					R.layout.ingredient_item, null);

			int viewWidth = createDishForm.getWidth()
					- createDishForm.getPaddingRight();

			TextView ingNameTextView = new TextView(this);
			ingNameTextView.setText(selected);
			ingNameTextView.setTextColor(Color.BLACK);
			ingredientItem.addView(ingNameTextView, viewWidth * 3 / 4,
					LayoutParams.FILL_PARENT);

			ImageView ingCloseButton = new ImageView(this);
			ingCloseButton.setTag(selected);
			ingCloseButton.setImageResource(R.drawable.close);
			ingCloseButton.setOnClickListener(this);
			ingredientItem.addView(ingCloseButton, viewWidth / 4,
					LayoutParams.FILL_PARENT);

			createDishForm.addView(ingredientItem);
			createDishForm.invalidate();
		} else {
			Toast.makeText(this, getString(R.string.ingredient_already_added),
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * This is the {@link OnEditorActionListener} for the ingredient
	 * {@link AutoCompleteTextView}. This is fired whenever the user clicks some
	 * action button while the ingredient text field is in focus. This method
	 * will just check whether the ingredient the user is trying to input
	 * exists. If not then a {@link Toast} message is displayed to notify the
	 * user.
	 * 
	 * @param v
	 * @param actionId
	 * @param event
	 * 
	 * @return boolean
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		String text = v.getText().toString();
		if (!text.equals("")) {
			String firstChar = text.charAt(0) + "";
			text = text.replaceFirst(firstChar, firstChar.toUpperCase());
			if (ingredientNameAutoCompleteAdapter.getPosition(text) == -1) {
				Toast.makeText(getBaseContext(),
						getString(R.string.ingredient_not_found),
						Toast.LENGTH_LONG).show();
			}
		}
		return false;
	}

	/**
	 * This is the {@link OnClickListener} for the {@link DialogInterface} that
	 * will let the user choose between the Gallery or the Camera for uploading
	 * the photo and calls the corresponding method.
	 */
	@Override
	public void onClick(DialogInterface dialog, int item) {
		if (item == GALLERY) {
			startGallery();
		} else {
			if (item == CAMERA) {
				startCamera();
			} else {
				Toast.makeText(getApplicationContext(),
						getString(R.string.error), Toast.LENGTH_SHORT).show();
			}
		}
	}

	/**
	 * This is the {@link android.view.View.OnClickListener} for the ingredient
	 * close button image view. This method will remove the ingredient from the
	 * view and add it back to the {@link AutoCompleteTextView} {@link Adapter}
	 * of the ingredients.
	 * 
	 * @param v
	 */
	@Override
	public void onClick(View v) {
		String ingredient = (String) v.getTag();
		createDishForm.removeView((View) v.getParent());
		ingredientNameAutoCompleteAdapter.add(ingredient);
		selectedIngredients.remove(ingredient);
	}

	/**
	 * This is the {@link AsyncTask} task that retrieves the restaurant list
	 * from the server. It runs in the background and show a
	 * {@link ProgressDialog} while the list is being loaded. The result of the
	 * task is an {@link ArrayList} of {@link String} that contains the
	 * restaurant names.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	class RestaurantAsyncTask extends AsyncTask<Void, Void, ArrayList<String>> {

		private ProgressDialog pd;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pd = ProgressDialog.show(CreateDishActivity.this, "",
					getString(R.string.getting_all_restaurants), true);
		}

		@Override
		protected ArrayList<String> doInBackground(Void... params) {
			return Restaurants.getAll();
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			pd.dismiss();
		}

		@Override
		protected void onPostExecute(ArrayList<String> result) {
			super.onPostExecute(result);
			pd.dismiss();
		}

	}

	/**
	 * This is the {@link AsyncTask} task that retrieves the ingredient list
	 * from the server. It runs in the background and show a
	 * {@link ProgressDialog} while the list is being loaded. The result of the
	 * task is an {@link ArrayList} of {@link String} that contains the
	 * ingredient names.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	class IngredientsAsyncTask extends AsyncTask<Void, Void, ArrayList<String>> {

		private ProgressDialog pd;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pd = ProgressDialog.show(CreateDishActivity.this, "",
					getString(R.string.getting_all_ingredients), true);
		}

		@Override
		protected ArrayList<String> doInBackground(Void... params) {
			return Ingredients.getAll();
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			pd.dismiss();
		}

		@Override
		protected void onPostExecute(ArrayList<String> result) {
			super.onPostExecute(result);
			pd.dismiss();
		}

	}

	// /**
	// * Overridden onSaveInstanceState(). Save UI state changes to the
	// * savedInstanceState. This bundle will be passed to onCreate if the
	// process
	// * is killed and restarted in order not to lose the data of the form.
	// *
	// * @param outState
	// *
	// * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	//
	// @Override
	// protected void onSaveInstanceState(Bundle outState) {
	// super.onSaveInstanceState(outState);
	// outState.putString("dishName", dishNameTextView.getText().toString());
	// outState.putString("dishType", dishTypeSpinner.getSelectedItem()
	// .toString());
	// outState.putString("restaurantName", restaurantNameField.getText()
	// .toString());
	// outState.putFloat("dishRating", dishRatingBar.getRating());
	// // outState.putStringArray("ingredients",
	// // (String[]) selectedIngredients.toArray());
	// outState.putDoubleArray("longitudes", longitudes);
	// outState.putDoubleArray("latitudes", latitudes);
	// dishName = dishNameTextView.getText().toString();
	// outState.putSerializable("hello", dishName);
	// }

	// /**
	// * Overridden onRestoreInstanceState(). Restore UI state that was last
	// * saved. The data is found inside the savedInstanceState {@link Bundle}.
	// *
	// * @param savedInstanceState
	// *
	// * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	//
	// @Override
	// protected void onRestoreInstanceState(Bundle savedInstanceState) {
	// super.onRestoreInstanceState(savedInstanceState);
	// Toast.makeText(this, "restored", Toast.LENGTH_SHORT).show();
	// dishNameTextView.setText(savedInstanceState.getString("dishName"));
	// }

	// @Override
	// protected void onPause() {
	// super.onPause();
	// onSaveInstanceState(new Bundle());
	// }
	//
	// @Override
	// protected void onResume() {
	// super.onResume();
	// restoreData();
	// }
	//
	// private void restoreData() {
	// Toast.makeText(this, dishName, Toast.LENGTH_SHORT).show();
	// }

}